﻿using IServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.Dto;
using Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Web.Controllers
{
    [Route("[controller]")]
    public class UserController : Controller
    {
        #region 接口注入
        readonly IUserServices _iUserServices;
        public UserController(IUserServices iUserServices)
        {
            _iUserServices = iUserServices;
        }
        #endregion

        #region 跳转页面
        public ActionResult Index()
        {
            // 跳转视图页面
            return View("~/views/Sys/User.cshtml");
        }
        #endregion


        #region 获取分页用户列表    
        /// <summary>
        /// 获取分页用户列表
        /// </summary>
        /// <param name="page">当前页</param>
        /// <param name="limit">当前页多少条数据</param>
        /// <returns></returns>
        [Route("GetPagedList")]
        [HttpGet]
        public async Task<IActionResult> GetPagedList(int page, int limit)
        {

            PageDto<User> result = new PageDto<User>();
            // where条件
            Expression<Func<User, bool>> whereLambda = p => p.Delflage == 0;
            // 排序字段
            Expression<Func<User, String>> orderByLambda = u => u.UserCode;
            bool isAsc = true;
            PageDto<User> list = await _iUserServices.GetPagedList(whereLambda, orderByLambda, isAsc, page, limit);

            return Json(list);
        }
        #endregion



        #region 新增用户 
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <returns></returns>
        [Route("Add")]
        [HttpPost]
        public async Task<IActionResult> Add([FromBody] User user)
        {
            // 声明返回对象
            ResultData<User> result = new ResultData<User>();

            // 验证用户名是否重复          
            // where条件
            Expression<Func<User, bool>> whereLambda = p => p.Delflage == 0 && p.UserCode == user.UserCode;
            User cheUser = await _iUserServices.GetByIdFirstModel(whereLambda);
            if (cheUser != null)
            {
                return Json(new ResultData<User>("1", "用户名不能重复！"));
            }

            // 给给用户赋共同值
            user.UserId = Guid.NewGuid().ToString();
            user.CreateTime = DateTime.Now;
            user.UpdateTime = DateTime.Now;
            user.Delflage = 0;

            // 添加方法执行
            bool res = await _iUserServices.Add(user);

            // 返回数据
            result.Code = res ? "0" : "1";
            result.ErrMsg = res ? "保存成功" : "保存失败";
            return Json(result);
        }
        #endregion


        #region 修改用户 
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <returns></returns>
        [Route("Edit")]
        [HttpPut]
        public async Task<IActionResult> Edit([FromBody] User user)
        {
            // 声明返回对象
            ResultData<User> result = new ResultData<User>();
            user.UpdateTime = DateTime.Now;
            // 执行修改方法
            int res = await _iUserServices.ModifyByKey(user);
            // 返回数据
            result.Code = res > 0 ? "0" : "1";
            result.ErrMsg = res > 0 ? "保存成功" : "保存失败";
            return Json(result);
        }
        #endregion


        #region 删除用户 
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <returns></returns>
        [Route("Delete")]
        [HttpDelete]
        public async Task<IActionResult> Delete([FromBody] User user)
        {
            // 声明返回对象
            ResultData<User> result = new ResultData<User>();
            // 执行删除方法
            int res = await _iUserServices.Delete(user);
            // 返回数据
            result.Code = res > 0 ? "0" : "1";
            result.ErrMsg = res > 0 ? "保存成功" : "保存失败";
            return Json(result);
        }
        #endregion

    }
}
